<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>类型</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="../css/font.css">
    <link rel="stylesheet" href="../css/xadmin.css">
    <link rel="stylesheet" href="../lib/layui/css/layui.css">
    <script src="../lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../js/xadmin.js"></script>
    <script src="../lib/axios.min.js"></script>
    <script src="../lib/FileSaver.min.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <script>
        // 是否开启刷新记忆tab功能
        // var is_remember = false;
    </script>
    <style>
        .layui-table-cell{
            overflow: visible;
        }
        .layui-layer-tips{
            display: none
        }
        /* 设置下拉框的高度与表格单元相同 */
        td .layui-form-select{
            margin-top: -3px;
            margin-left: -15px;
            margin-right: -15px;
        }
        /*解决下拉框被表格覆盖样式问题*/
        .layui-form-select dl {
            z-index: 9999;
        }
        .layui-table-box {
            overflow: visible
        }
        .layui-table-body{
            overflow:visible;
        }
    </style>
</head>
<body>
    <div class="layui-fluid">
        <div class="layui-row">
            <br/><br/>
            <form class="layui-form" id="mainForm">
                <div class="layui-form-item">
                    <label class="layui-form-label">全宗名称</label>
                    <div class="layui-input-inline">
                        <div id="qzv"></div>
                    </div>
                    <label class="layui-form-label">档案类型</label>
                    <div class="layui-input-inline">
                        <div id="kindv"></div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="new" class="layui-form-label">
                        类型名称
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="new" name="new"
                        autocomplete="off" class="layui-input">
                    </div>
                    <label for="new" class="layui-form-label">
                        管理模式
                    </label>
                    <div class="layui-input-inline">
                        <select name="cj" lay-filter="sortC">
                            <option value=1>按件保管</option>
                            <option value=2>案卷 / 卷内</option>
                            <option value=3>项目 / 案卷 / 卷内</option>
                        </select>
                    </div>
                    <button type="button" class="layui-btn" lay-filter="inc" lay-submit="">
                        新增
                    </button>
                </div>
            
                <div class="layui-form-item">
                    <label for="" class="layui-form-label">
                        当前层级
                    </label>
                    <div class="layui-input-inline" id="cv"></div>
                    <div class="layui-inline">
                        <label for="L_repass" class="layui-form-label">
                        </label>
                        <button type="button" class="layui-btn layui-btn-normal" lay-filter="save" lay-submit="">
                            保存
                        </button>
                        <button type="button" class="layui-btn layui-btn-danger" lay-filter="del" lay-submit="">
                            删除
                        </button>
                        &nbsp;&nbsp;&nbsp;
                        <button class="layui-btn layui-btn-warm" type="button" onclick="backup();">
                            <i class="icon iconfont">&#xe714;</i>
                        </button>
                        &nbsp;&nbsp;&nbsp;
                        <div style="position: relative; display: inline;">
                            <input id="upload-input" style="position: absolute; top: 0; bottom: 0; left: 0;right: 0; opacity: 0; display: inline;" type="file" accept="application/json" onchange="recovery(this)" />
                            <!-- 自定义按钮效果 -->
                            <div style="text-align: top; display: inline;">
                                <i class="layui iconfont">&#xe71d;</i>
                            </div>
                        </div>
                    </div> 

                </div>   
            </form>
            <form class="layui-form" id="nextForm">
                <br/>
                <div class="layui-form-item">
                    <label for="field" class="layui-form-label">
                        字段码
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="field" name="field"
                        autocomplete="off" class="layui-input">
                    </div>
                    <label for="text" class="layui-form-label">
                        字段名
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="title" name="title"
                        autocomplete="off" class="layui-input">
                    </div>
                    <button  class="layui-btn" lay-filter="add" lay-submit="">
                        添加
                    </button>
                </div>
            </form>
        </div>
        <div class="layui-row">
        <table id="t1" lay-filter="tab1"></table></div>
    </div>
    <script type="text/html" id="cm">
        <select name="ci" lay-filter="typeC">
            <option value=1>文件</option>
            {{# if(c==2){ }}
            <option value=2>案卷</option>
            {{# }else if(c==3){ }}
            <option value=2>案卷</option>
            <option value=3>项目</option>
            {{# } }}
        </select>
    </script>
    <script id="qzm" type="text/html">
        <select name="qid" id="qid" lay-filter="qzType">
            {{#  layui.each(qz, function(index, item){ }}
            <option value="{{item.qid}}">{{item.title}}</option>
            {{#  }); }}
            {{#  if(qz.length === 0){ }}
                无数据
            {{#  } }}
        </select>
    </script>
    <script id="kindm" type="text/html">
        <select name="kid" lay-filter="kindType">
            {{#  layui.each(kind, function(index, item){ }}
            <option value="{{item.kid}}">{{item.kname}}</option>
            {{#  }); }}
            {{#  if(kind.length === 0){ }}
                无数据
            {{#  } }}
        </select>
    </script>
    <script type="text/html" id="bar">
        <a lay-event="update" title="编辑" href="javascript:;">
            <i class="layui-icon">&#xe642;</i></a>
        <a lay-event="delete" title="删除" href="javascript:;">
            <i class="layui-icon">&#xe640;</i></a>
        <a lay-event="moveup" title="上移" href="javascript:;">
            <i class="layui-icon">&#xe619;</i></a>
        <a lay-event="movedown" title="下移" href="javascript:;">
            <i class="layui-icon">&#xe61a;</i></a>
    </script>
    <script type="text/html" id="switchTpl">
        <input type="checkbox" name="sort" value="{{d.sort}}" lay-skin="switch" lay-text="是|否" lay-filter="sort" {{ d.sort == true ? 'checked' : '' }}>
    </script>
    <script type="text/html" id="requTpl">
        <input type="checkbox" name="required" value="{{d.required}}" lay-skin="switch" lay-text="是|否" lay-filter="required" {{ d.required == true ? 'checked' : '' }}>
    </script>
    <script type="text/html" id="typeTpl">
        <select name="type" lay-filter="type" data-value="{{ d.type }}">
            <option value="0">文本框</option>
            <option value="1">长文本框</option>
            <option value="2">可选项</option>
            <option value="3">文本域</option>
            <!-- <option value="4">存放地</option> -->
        </select>
    </script>
    <script type="text/html" id="addtoTpl">
        <select name="addto" lay-filter="addto" data-value="{{ d.addto }}">
            <option value="0">空</option>
            <option value="1">复制</option>
            <option value="2">递增</option>
        </select>
    </script>
    <script type="text/javascript">
        var qz = kind = kin = [];
        var c = 1;
        var cj2 = cj3 = [];
        const def = [{'field':'DH', 'title':'档号', "type": "0", "required": true,"addto": "0", "sort": true, "fixed": "left"},
        {'field':'TM', 'title':'题名', "type": "0", "required": true,"addto": "0", "sort": false}];
        var datab = {};
        //全宗
        axios.get('/api/get_q').then(res=>{
            var tree = res.data;
            for (var i=0;i<tree.length;i++){
                if (tree[i].parentId === "0") {
                    qz.push(tree[i]);
                }
            }
            layui.config({
                base: '../lib/layui/layui-expand/' //插件路径
            }).extend({
                enhanceform: 'enhanceform'
            });
            layui.use(['laytpl', 'form', 'table', 'enhanceform'], function(){
                var laytpl = layui.laytpl
                ,form = layui.form
                ,table = layui.table
                ,enhanceForm = layui.enhanceform;
                var enhance = new enhanceForm({
                    elem: '#mainForm' //表单选择器
                });
                
                var getTplq = qzm.innerHTML
                ,qzv = document.getElementById('qzv')
                ,getTplk = kindm.innerHTML
                ,kindv = document.getElementById('kindv')
                ,getTplc = cm.innerHTML
                ,cv = document.getElementById('cv');
                laytpl(getTplq).render(qz, function(html){
                    qzv.innerHTML = html;
                });
                laytpl(getTplc).render(c, function(html){
                    cv.innerHTML = html;
                });
                enhance.setSelectVal('ci', 1);
                //类型
                var col = [[
                            {field: 'field', title: '字段码'}
                            ,{field: 'title', title: '字段名'}
                            ,{field: 'type', title: '交互类型', templet: '#typeTpl'}
                            ,{field: 'required', title: '必填项', templet: '#requTpl', width:87, unresize: true}
                            ,{field: 'addto', title: '追加', templet: '#addtoTpl'}
                            ,{field: 'sort', title: '列表排序', width:87, templet: '#switchTpl', unresize: true}
                            ,{field: '', title: '操作', toolbar: '#bar'}        
                        ]];
                axios.post('/api/get_k_q', {qid: Number(qz[0].qid)}).then(res=>{
                    kind = res.data;
                    if(kind.length > 0){
                        kin = kind[0];
                        c = kin.cj;
                    }else{
                        kind.push({kid: 0, qid: Number(qz[0].qid), kname: '', head:[]});
                        kin = kind[0];
                    }
                    laytpl(getTplk).render(kind, function(html){
                        kindv.innerHTML = html;
                    });
                    enhance.setSelectVal('cj', c);
                    laytpl(getTplc).render(c, function(html){
                        cv.innerHTML = html;
                    });
                    form.render('select');
                    datab = kin.head;
                    //初始字段
                    table.render({
                        elem: '#t1'
                        ,width: 700
                        ,height: 340
                        ,cols: col
                        ,data: datab
                        ,done: function (res, curr, count) {
                        // 渲染之前组装select的option选项值 
                            layui.each($("select[name='type']"), function (index, item) {
                                var elem = $(item);
                                elem.val(elem.data('value'));
                            });
                            layui.each($("select[name='addto']"), function (index, item) {
                                var elem = $(item);
                                elem.val(elem.data('value'));
                            });
                            form.render('select');
                        }
                    });
                   
                });
                form.on('switch(sort)', function(obj){
                    var elem = obj.othis.parents('tr');
                    var dataindex = elem.attr("data-index");
                    var tabledata = table.cache["t1"];
                    $.each(tabledata,function(index,value){
                        if(value.LAY_TABLE_INDEX==dataindex){
                            tabledata[index]['sort'] = obj.elem.checked;
                        }  
                    });
                })
                //监听必填
                form.on('switch(required)', function(obj){    
                    var elem = obj.othis.parents('tr');
                    var dataindex = elem.attr("data-index");
                    var tabledata = table.cache["t1"];
                    $.each(tabledata,function(index,value){
                        if(value.LAY_TABLE_INDEX==dataindex){
                            tabledata[index]['required'] = obj.elem.checked;
                        }  
                    });
                });
                //监听交互
                form.on('select(type)', function(obj){
                    var elem = obj.othis.parents('tr');
                    var dataindex = elem.attr("data-index");
                   
                    var tabledata = table.cache["t1"];
                    $.each(tabledata,function(index,value){
                        if(value.LAY_TABLE_INDEX==dataindex){
                            tabledata[index]['type'] = obj.value;
                            if(obj.value == "3"){
                                tabledata[index]['addto'] = "1";
                                table.reload('t1', {
                                    data: tabledata
                                })
                            }
                        }  
                    });
                });
                //监听追加
                form.on('select(addto)', function(obj){
                    var elem = obj.othis.parents('tr');
                    var dataindex = elem.attr("data-index");
                
                    var tabledata = table.cache["t1"];
                    $.each(tabledata,function(index,value){
                        if(value.LAY_TABLE_INDEX==dataindex){
                            tabledata[index]['addto'] = obj.value;
                        }  
                    });
                });
                //选择全宗
                form.on("select(qzType)", function(data){
                    var qval = Number(data.value);
                    axios.post('/api/get_k_q', {qid: qval}).then(res=>{
                        kind = res.data;

                        if(kind.length > 0){
                            kin = kind[0];
                        }else{
                            kind.push({kid: 0, qid: qval, cj: 1, kname: '', head:[]});
                            kin = kind[0];
                        }
                        
                        laytpl(getTplk).render(kind, function(html){
                            kindv.innerHTML = html;
                        });
                        c = kin.cj;
                        enhance.setSelectVal('cj', c);
                        laytpl(getTplc).render(c, function(html){
                            cv.innerHTML = html;
                        });
                        form.render('select');
                        datab = kin.head;
                        table.render({
                            elem: '#t1'
                            ,width: 700
                            ,height: 340
                            ,cols: col
                            ,data: datab
                            ,done: function (res, curr, count) {
                            // 渲染之前组装select的option选项值 
                                layui.each($("select[name='type']"), function (index, item) {
                                    var elem = $(item);
                                    elem.val(elem.data('value'));
                                });
                                layui.each($("select[name='addto']"), function (index, item) {
                                    var elem = $(item);
                                    elem.val(elem.data('value'));
                                });
                                form.render('select');
                            }
                        })
                        
                    });
                })
                //添加类型
                form.on('submit(inc)', function(dat){
                    var formData = dat.field;
                    c = Number(formData.cj);
                    if(formData.kid !== "0"){
                        if(c == '1'){
                            kind.push({kid: 0, qid: Number(formData.qid), cj: c, kname: formData.new, head:def});
                        }else{
                            kind.push({kid: 0, qid: Number(formData.qid), cj: c, kname: formData.new, head:[{'field':'DH', 'title':'档号', "type": "0", "required": true,"addto": "0", "sort": true, "fixed": "left"},
                    {'field':'TM', 'title':'题名', "type": "0", "required": true,"addto": "0", "sort": false},
                    {'field':'PID', 'title':'所属卷号', "type": "0", "required": true,"addto": "0", "sort": true}]});
                        }
                    }else {
                        kind.pop();
                        if(c == '1'){
                            kind.push({kid: 0, qid: Number(formData.qid), cj: c, kname: formData.new, head:def});
                        }else{
                            kind.push({kid: 0, qid: Number(formData.qid), cj: c, kname: formData.new, head:[{'field':'DH', 'title':'档号', "type": "0", "required": true,"addto": "0", "sort": true, "fixed": "left"},
                    {'field':'TM', 'title':'题名', "type": "0", "required": true,"addto": "0", "sort": false},
                    {'field':'PID', 'title':'所属卷号', "type": "0", "required": true,"addto": "0", "sort": true}]});
                        }
                    }
                    
                    kin = kind[kind.length - 1];
                    laytpl(getTplk).render(kind, function(html){
                        kindv.innerHTML = html;
                    });
              
                    enhance.setSelectVal('kid', "0");
                    enhance.setSelectVal('cj', c);
                    laytpl(getTplc).render(c, function(html){
                        cv.innerHTML = html;
                    });
                    form.render('select');
                    datab = kin.head;
                    table.reload('t1', {
                        data: datab
                    })
                    return false;
                });
                //选择类型
                form.on('select(kindType)', function(data){   
                    var val = Number(data.value);
                    var ki = kind.filter(item=>{
                        return item.kid == val;
                    });
                    kin = ki[0];
                    c = kin.cj;
                    enhance.setSelectVal('cj', c);
                    laytpl(getTplc).render(c, function(html){
                        cv.innerHTML = html;
                    });
                    form.render('select');
                    datab = kin.head;
                    table.reload('t1', {
                        data: datab
                        ,done: function (res, curr, count) {
                        // 渲染之前组装select的option选项值 
                            layui.each($("select[name='type']"), function (index, item) {
                                var elem = $(item);
                                elem.val(elem.data('value'));
                            });
                            layui.each($("select[name='addto']"), function (index, item) {
                                var elem = $(item);
                                elem.val(elem.data('value'));
                            });
                            form.render('select');
                        }
                    });
                    return false;
                });
                //选择层级
                form.on("select(sortC)", function(data){
                    c = Number(data.value);
                    laytpl(getTplc).render(c, function(html){
                        cv.innerHTML = html;
                    })
                    form.render('select');
                    if(c>1){
                        kin.head = [{'field':'DH', 'title':'档号', "type": "0", "required": true,"addto": "0", "sort": true, "fixed": "left"},
                        {'field':'TM', 'title':'题名', "type": "0", "required": true,"addto": "0", "sort": false},
                        {'field':'PID', 'title':'所属卷号', "type": "0", "required": true, "addto": "2", "sort": true}];
                        datab = kin.head;
                        table.reload('t1',{
                            data: datab
                            ,done: function (res, curr, count) {
                            // 渲染之前组装select的option选项值 
                                layui.each($("select[name='type']"), function (index, item) {
                                    var elem = $(item);
                                    elem.val(elem.data('value'));
                                });
                                layui.each($("select[name='addto']"), function (index, item) {
                                    var elem = $(item);
                                    elem.val(elem.data('value'));
                                });
                                form.render('select');
                            }
                        })
                    }else{
                        kin.head = def;
                        datab = kin.head;
                        table.reload('t1',{
                            data: datab
                            ,done: function (res, curr, count) {
                            // 渲染之前组装select的option选项值 
                                layui.each($("select[name='type']"), function (index, item) {
                                    var elem = $(item);
                                    elem.val(elem.data('value'));
                                });
                                layui.each($("select[name='addto']"), function (index, item) {
                                    var elem = $(item);
                                    elem.val(elem.data('value'));
                                });
                                form.render('select');
                            }
                        })
                    }
                })
                form.on("select(typeC)", function(data){
                    if(data.value == '1'){
                        datab = kin.head;
                        table.reload('t1', {
                            data: datab
                            ,done: function (res, curr, count) {
                            // 渲染之前组装select的option选项值 
                                layui.each($("select[name='type']"), function (index, item) {
                                    var elem = $(item);
                                    elem.val(elem.data('value'));
                                });
                                layui.each($("select[name='addto']"), function (index, item) {
                                    var elem = $(item);
                                    elem.val(elem.data('value'));
                                });
                                form.render('select');
                            }
                        })
                    }else if (data.value == '2'){
                        axios.post('/api/get_kc', {qid:-2, kname:String(kin.kid)}).then(res=> {
                            if (res.data.length == 0){
                                if(c==2){
                                    cj2 = {head:def, qid:-2, kname:String(kin.kid),cj:0,kid:0};
                                }else{
                                    cj2 = {head:[{'field':'DH', 'title':'档号', "type": "0", "required": true,"addto": "0", "sort": true, "fixed": "left"},
                                    {'field':'TM', 'title':'题名', "type": "0", "required": true,"addto": "0", "sort": false},
                                    {'field':'PID', 'title':'所属项目号', "type": "0", "required": true, "addto": "2", "sort": true}],
                                    qid:-2, kname:String(kin.kid),cj:0,kid:0};
                                }
                            }else{
                                cj2 = res.data[0];
                            }
                            datab = cj2.head;
                            table.reload('t1', {
                                data: datab
                                ,done: function (res, curr, count) {
                                // 渲染之前组装select的option选项值 
                                    layui.each($("select[name='type']"), function (index, item) {
                                        var elem = $(item);
                                        elem.val(elem.data('value'));
                                    });
                                    layui.each($("select[name='addto']"), function (index, item) {
                                        var elem = $(item);
                                        elem.val(elem.data('value'));
                                    });
                                    form.render('select');
                                }
                            })
                        })
                    }else if (data.value == '3'){
                        axios.post('/api/get_kc', {qid:-3, kname:String(kin.kid)}).then(res=> {
                            if (res.data.length == 0){
                                cj3 = {head:def, qid:-3, kname:String(kin.kid),cj:0,kid:0};
                            }else{
                                cj3 = res.data[0];
                            }
                            datab = cj3.head;
                            table.reload('t1', {
                                data: datab
                                ,done: function (res, curr, count) {
                                // 渲染之前组装select的option选项值 
                                    layui.each($("select[name='type']"), function (index, item) {
                                        var elem = $(item);
                                        elem.val(elem.data('value'));
                                    });
                                    layui.each($("select[name='addto']"), function (index, item) {
                                        var elem = $(item);
                                        elem.val(elem.data('value'));
                                    });
                                    form.render('select');
                                }
                            })
                        })
                    }
                })
                //添加字段
                form.on('submit(add)', function(data) {
                    var formData = data.field;
                    formData.sort = false;
                    formData.type = "0";
                    formData.required = false;
                    formData.addto = "0";
                    datab.push(formData);
                    kin.head = datab;
                    table.reload('t1', {
                        data: datab
                        ,done: function (res, curr, count) {
                        // 渲染之前组装select的option选项值 
                            layui.each($("select[name='type']"), function (index, item) {
                                var elem = $(item);
                                elem.val(elem.data('value'));
                            });
                            layui.each($("select[name='addto']"), function (index, item) {
                                var elem = $(item);
                                elem.val(elem.data('value'));
                            });
                            form.render('select');
                        }
                    })
                    return false;
                });
                //保存类型
                form.on('submit(save)', function(data) {
                    var formData = data.field;
                    var val = Number(formData.kid);
                    var datab = layui.table.cache['t1'];
                    if(formData.ci == "1"){
                        kin.head = datab;
                        if (val === 0) {
                            delete(kin.kid);
                            axios.post('/api/add_k', kin).then(res=>{
                                kin.kid = res.data.kid;
                                layer.msg('档案类型保存成功', {icon: 1,time:1000});
                                laytpl(getTplk).render(kind, function(html){
                                    kindv.innerHTML = html;
                                });
                                form.render("select");
                                enhance.setSelectVal('kid', kin.kid);
                                });
                        }else {
                            kin.cj = Number(formData.cj);
                            axios.post('/api/up_k', kin).then(res=>{
                                layer.msg('档案类型保存成功', {icon: 1,time:1000});
                            });
                        }
                    }else if(formData.ci == "2"){
                        cj2.head = datab;
                        if(cj2.kid == 0){
                            delete(cj2.kid);
                            axios.post('/api/add_k', cj2).then(res=>{
                                cj2.kid = res.data.kid;
                                layer.msg('档案类型保存成功', {icon: 1,time:1000});
                            })
                        }else{
                            axios.post('/api/up_k', cj2).then(res=>{
                                layer.msg('档案类型保存成功', {icon: 1,time:1000});
                            });
                        }   
                    }else if(formData.ci == "3"){
                        cj3.head = datab;
                        if(cj3.kid == 0){
                            delete(cj3.kid);
                            axios.post('/api/add_k', cj3).then(res=>{
                                cj3.kid = res.data.kid;
                                layer.msg('档案类型保存成功', {icon: 1,time:1000});
                            })
                        }else{
                            axios.post('/api/up_k', cj3).then(res=>{
                                layer.msg('档案类型保存成功', {icon: 1,time:1000});
                            });
                        }  
                    }
                    
                    return false;
                });
                //删除类型
                form.on('submit(del)', function(data) {
                    var formData = data.field;
                    let qval = Number(formData.qid);
                    let dk = {kid: Number(formData.kid)}
                    layer.confirm('确认要删除档案类型吗？',function(index){
                        axios.post('/api/del_k', dk).then(res=>{
                            console.log(res.data)
                            if(res.data.cj>1){
                                axios.post('/api/del_kn', {kname: String(res.data.kid)});
                            }
                            kind.pop(res.data)
                            laytpl(getTplk).render(kind, function(html){
                                kindv.innerHTML = html;
                            });
                            form.render("select");
                            if(kind.length > 0){
                                kin = kind[0];
                            }else{
                                kind.push({kid: 0, qid: Number(qval), kname: '', head:[]});
                                kin = kind[0];
                            }
                            datab = kin.head;
                            table.reload('t1', {
                                data: datab
                                ,done: function (res, curr, count) {
                                // 渲染之前组装select的option选项值 
                                    layui.each($("select[name='type']"), function (index, item) {
                                        var elem = $(item);
                                        elem.val(elem.data('value'));
                                    });
                                    layui.each($("select[name='addto']"), function (index, item) {
                                        var elem = $(item);
                                        elem.val(elem.data('value'));
                                    });
                                    form.render('select');
                                }
                            })
                            layer.msg('档案类型已删除', {icon: 1,time:1000});
                        });
                    });
                    
                    return false;
                });
                //表格操作
                table.on('tool(tab1)',
                function(obj){
                    var index = Number(obj.tr[0].dataset.index);
                    switch (obj.event) {
                    case 'moveup':
                        var data = obj.data;
                        if(index!=0){
                            datab[index] = datab.splice(index-1, 1, datab[index])[0];
                        }else{
                            datab.push(datab.shift());
                        }
                        table.reload('t1',{
                            data: datab
                        })
                        break;
                    case 'movedown':
                        if(index!=datab.length-1){
                            datab[index] = datab.splice(index+1, 1, datab[index])[0];
                        }else{
                            datab.unshift(datab.splice(index,1)[0]);
                        }
                        table.reload('t1',{
                            data: datab
                        })
                        break;
                    case 'delete':
                        layer.confirm('确认要删除行吗？',function(){
                            datab.splice(index, 1);
                            obj.del();
                            layer.msg('删除成功', {icon: 1, time: 1000});
                        });
                        break;
                    case 'update':
                        datab.splice(index, 1);
                        obj.del();
                        var data = obj.data;
                        var enhancen = new enhanceForm({
                            elem: '#nextForm'
                        })
                        enhancen.filling({field: data.field, title: data.title})
                        break;
                    }
                })
            })
        })
        //备份
        function backup() {
            let content = JSON.stringify(layui.table.cache['t1']);
            var blob = new Blob([content], {type: "text/plain;charset=utf-8"});
            saveAs(blob, "kind.json");
            return false;
        };
        //还原
        function recovery(input) {
            var reader = new FileReader();
            var file = input.files[0];
            reader.readAsText(file);
            reader.onload = function() {
                layui.use('table', function(){
                    datab = JSON.parse(reader.result)
                    layui.table.reload('t1', {
                        data: datab
                        ,done: function (res, curr, count) {
                        // 渲染之前组装select的option选项值 
                            layui.each($("select[name='type']"), function (index, item) {
                                var elem = $(item);
                                elem.val(elem.data('value'));
                            });
                            layui.each($("select[name='addto']"), function (index, item) {
                                var elem = $(item);
                                elem.val(elem.data('value'));
                            });
                            form.render('select');
                        }
                    })
                })
                
            }
            return false;
        };
    </script>
</body>
</html>
